package com.wcar.examine.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wcar.common.entity.*;
import com.wcar.common.util.WebUtil;
import com.wcar.common.vo.Result;
import com.wcar.examine.dao.*;
import com.wcar.examine.service.ExamineService;
import com.wcar.examine.vo.ExamineVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ExamineServiceImpl implements ExamineService {
    @Autowired
    private ExamineDao examineDao;
    @Resource
    private UserDao userDao;
    @Autowired
    private StoreUserDao storeUserDao;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private ProductDao productDao;
    @Resource
    private UserRoleDao userRoleDao;
    @Override
    public Result<Page<ProductExamine>> selectExamine(ExamineVo examineVo) {
        //获取token
        String token = WebUtil.getToken();
        ValueOperations<String, String> forValue = redisTemplate.opsForValue();
        String s = forValue.get("user:login:" + token);
        User user = JSON.parseObject(s, User.class);
        QueryWrapper<StoreUser> storeUserWrapper = new QueryWrapper<>();
        QueryWrapper<UserRole> wrapper1=new QueryWrapper<>();
        wrapper1.eq("user_id",user.getUserId());
        List<UserRole> userRoles = userRoleDao.selectList(wrapper1);
        ArrayList<Integer> roleIds=new ArrayList<>();
        for (UserRole u: userRoles) {
            roleIds.add(u.getRoleId());
        };
        if(!roleIds.contains(1)){
            storeUserWrapper.eq("user_id",user.getUserId());
        }
        List<StoreUser> storeUsers = storeUserDao.selectList(storeUserWrapper);
        ArrayList<Integer> userIds = new ArrayList<>();
        for (StoreUser storeUser : storeUsers) {
            userIds.add(storeUser.getStoreId());
        }
        Page<ProductExamine> page= new Page<>(examineVo.getCurrentPage(),examineVo.getPageSize());
        QueryWrapper<ProductExamine> wrapper = new QueryWrapper<>();
        if(examineVo.getName()!=null&&examineVo.getName()!=""){
            wrapper.like("name",examineVo.getName());
        }
        if (examineVo.getIsExamine()!=null){
            wrapper.eq("is_examine",examineVo.getIsExamine());
        }
        if(userIds.size()==0){
            return new Result<>(201,"获取订单信息成功",null);
        }
        wrapper.in("tpe.store_id",userIds);
        List<User> users = userDao.selectList(null);
        Page<ProductExamine> list = examineDao.getPage(page, wrapper);
        for (ProductExamine p:list.getRecords()){
            for (User u:users){
                if (u.getUserId().equals(p.getExamineId())){
                    p.setUserName(u.getUserName());
                }
            }
        }
        return new Result<>(200,"获取商品审核信息成功",list);
    }

    @Override
    public Result updateExamine(ProductExamine productExamine) {
        productExamine.setExamineTime(new Date());
        int i = examineDao.updateById(productExamine);
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        wrapper.eq("product_id",productExamine.getProductId());
        Product product = productDao.selectOne(wrapper);
        product.setExamineStatus(productExamine.getIsExamine());
        productDao.updateById(product);
        return new Result<>(200,"修改成功",i);
    }
}
